package com.tianbo.elfii.client.common;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.tianbo.elfii.client.common.ElfiiCommonUtil.FeedBackType;
import com.tianbo.elfii.client.common.wlanmanagement.BaseDialogPane;

public class WlanConnectPane extends BaseDialogPane {

	public JTextField inputField = null;
	public ElfiiCommonKeyboard keyboard = null;
	public ElfiiCommonConfigNetWifi wifiConfig = null;
	public WlanAP connectingAP = null;
	public JPanel dialog = null; 
	public static WlanConnectingThread  wlanConnectingThread = null;

	public WlanConnectPane(Object obj) {
		super();
		// TODO Auto-generated constructor stub
		
		wifiConfig = (ElfiiCommonConfigNetWifi)obj;
		connectingAP = wifiConfig.selectedApPanel.ap;
		
		if(connectingAP.getCurStat()==APSTAT.CONNECTED){
			initDisconnectDialog();
		}
		else{
			if(connectingAP.getEncryptedStat()){
				initPwConnectDialog();
				initKeyboardPane();
			
				((WlanPwConnectDialog)dialog).setRelatedKeyboard(keyboard);
			}
			else
			{
				initNoPwConnectDialog();
			}
		}
		
		dialog.setOpaque(false);
	}
	
	public void init(){
		
	}
	
	public void initDisconnectDialog(){
		dialog = new WlanDisconnectDialog(connectingAP);
		dialog.setBounds(240, 248, 545, 272);
		((WlanDisconnectDialog)dialog).init();
		((WlanDisconnectDialog)dialog).disconnectBtn.addActionListener(new DisconnectBtnListener());
		((WlanDisconnectDialog)dialog).cancelBtn.addActionListener(new CancelBtnListener());
		this.add(dialog);
	}
	
	public void initNoPwConnectDialog(){
		dialog = new WlanNoPwConnectDialog(connectingAP);
		dialog.setBounds(240, 248, 545, 272);
		((WlanNoPwConnectDialog)dialog).init();
		((WlanNoPwConnectDialog)dialog).cancelBtn.addActionListener(new CancelBtnListener());
		((WlanNoPwConnectDialog)dialog).confirmBtn.addActionListener(new NoPwConnectBtnListener());
		this.add(dialog);
	}
	
	public void initPwConnectDialog(){
		dialog = new WlanPwConnectDialog(connectingAP);
		dialog.setBounds(240, 80, 545, 272);
		if(connectingAP.getCurStat()==APSTAT.CONNECTING){
			
			if(connectingAP.connectRetFlag == 3){
				((WlanPwConnectDialog)dialog).init(false,connectingAP.getPassword());
			}
			else{
				((WlanPwConnectDialog)dialog).init(true,connectingAP.getPassword());
			}
		}
		else{
			if(connectingAP.getCurStat()==APSTAT.SAVED){
				((WlanPwConnectDialog)dialog).init(true,connectingAP.getPassword());
			}
			else{
				((WlanPwConnectDialog)dialog).init();
			}
		}
		((WlanPwConnectDialog)dialog).cancelBtn.addActionListener(new CancelBtnListener());
		((WlanPwConnectDialog)dialog).confirmBtn.addActionListener(new PwConnectBtnListener());
		this.add(dialog);
		inputField = ((WlanPwConnectDialog)dialog).pwInputField;
	}

	public void initKeyboardPane() {
		keyboard = new ElfiiCommonLoginKeyboard();
		
		keyboard.setTextField(inputField, 20);

		JPanel keyPanel = keyboard.createKeyPanel();
		keyPanel.setOpaque(false);

		this.add(keyPanel);
		keyPanel.setBounds(150, 380, 735, 300);

		keyboard.createInfoPanel();

	}
	
	public class CancelBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if (wifiConfig.selectedApPanel.ap != null) {
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
				mainFrame.setContentPane(backPanel);
				wifiConfig.setWifiListPane();
			}
		}
	}
	
	
	public class PwConnectBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if (connectingAP != null) {
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
				
				if(wlanConnectingThread!=null){
					wlanConnectingThread.cancel();
				}
				
				WlanPwConnectDialog pwConnectDialog = (WlanPwConnectDialog)dialog;
				connectingAP.setPassword(pwConnectDialog.pwInputField.getText());
				wlanConnectingThread = new WlanConnectingThread(wifiConfig);
				connectingAP.setCurStat(APSTAT.CONNECTING);
				wlanConnectingThread.start();
				mainFrame.setContentPane(backPanel);
			}
		}
	}
	
	public class NoPwConnectBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if (connectingAP != null) {
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
				
				if(wlanConnectingThread!=null){
					wlanConnectingThread.cancel();
				}
				
				wlanConnectingThread = new WlanConnectingThread(wifiConfig);
				connectingAP.setCurStat(APSTAT.CONNECTING);
				wlanConnectingThread.start();
				mainFrame.setContentPane(backPanel);
			}
		}
	}
	
	public class DisconnectBtnListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if (connectingAP != null) {
				ElfiiCommonUtil.playFeedbackAudio(FeedBackType.CLICK);
//				ElfiiCommonConfigUtil.disConnectWlan();
				
				if(connectingAP.getSavedStat()){

					connectingAP.setCurStat(APSTAT.SAVED);
					wifiConfig.selectedApPanel.str2 = "已保存";
				}
				else{
					connectingAP.setCurStat(APSTAT.UNCONNECTED);
					wifiConfig.selectedApPanel.str2 = "未连接";
				}
				
				ElfiiCommonConfigUtil.disConnectWlan();

				mainFrame.setContentPane(backPanel);

				wifiConfig.selectedApPanel.repaint();
				
				wifiConfig.connectedApPanel = null;


				try {
					Thread.sleep(1000);
				} catch (InterruptedException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				wifiConfig.setWifiListPane();
			}
		}
	}

}
